package etnorservice.etnor_service.controller;

import etnorservice.etnor_service.domain.po.ApiCheckLog;
import etnorservice.etnor_service.domain.po.ApiDTO;
import etnorservice.etnor_service.domain.po.ApiLog;
import etnorservice.etnor_service.domain.vo.ApiDetailsVO;
import etnorservice.etnor_service.result.Result;
import etnorservice.etnor_service.service.ApiCheckLogService;
import etnorservice.etnor_service.service.ApiCheckService;
import etnorservice.etnor_service.service.IApiService;
import etnorservice.etnor_service.utils.DateUtil;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.List;

@RestController
public class UrlCheckController {
    @Autowired
    ApiCheckService apiCheckService;
    @Autowired
    ApiCheckLogService apiCheckLogService;
    @Autowired
    IApiService apiService;
    @GetMapping("/check-url")
    public ResponseEntity<String> checkUrlStatus(@RequestParam("url") String url) throws MalformedURLException {
        Date date=new Date(System.currentTimeMillis());
        ApiCheckLog apiCheckLog=new ApiCheckLog();
        apiCheckLog.setLasttime(date);
        apiCheckLogService.updateLog(apiCheckLog);
        return apiCheckService.checkUrlStatus(url);
    }
    @GetMapping("/check-api")
    public Result apiCheck(@RequestParam("apiid") String apiid) throws JSONException {
        Result result = new Result();
        String testUrl=apiService.findTestUrl(apiid);
        ResponseEntity<String> stringResponseEntity = apiCheckService.checkUrlStatus(testUrl);
        ApiLog apiLog=new ApiLog();
        apiLog.setApiid(apiid);
        apiLog.setTime(new Date());
        apiLog.setResult(stringResponseEntity.toString());
        int statusCode = stringResponseEntity.getStatusCodeValue();
        apiLog.setCode(statusCode);
        apiService.saveApiLog(apiLog);
        result.setData(apiLog);
        return result;
    }
    @GetMapping("/api-logs")
    public Result getApiLogs(@RequestParam("apiid") String apiid) throws Exception {
        Result result = new Result();
        List<ApiLog> logs = apiService.findLogs(apiid);
        for(ApiLog apiLog:logs){
            System.out.println("日志"+apiLog);
        }
        result.setData(logs);
        return result;
    }
    @GetMapping("/getLastCheckTime")
    public Result getLastCheckTime() {
        Result result = new Result();
        ApiCheckLog apiCheckLog = apiCheckLogService.getApiCheckLog();
        // 假设apiCheckLog.getLasttime()是java.util.Date类型
        Instant instant = apiCheckLog.getLasttime().toInstant(); // 将Date转换为Instant
        ZoneId utcZone = ZoneId.of("UTC"); // 定义UTC时区
        ZonedDateTime utcZonedDateTime = instant.atZone(utcZone); // 转换为UTC时区的ZonedDateTime
        // 定义目标时区（例如，亚洲/上海）
        ZoneId asiaTimeZone = ZoneId.of("Asia/Shanghai");
        ZonedDateTime asiaTime = utcZonedDateTime.withZoneSameInstant(asiaTimeZone); // 转换为目标时区的ZonedDateTime
        result.setCode(200);
        result.setMessage("success");
        result.setData(asiaTime);
        return result;
    }

}
